#!/usr/bin/perl -w
########################################################################
# script name:  info_hosts.pl
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/25    0.1     MDUMONT         initial
########################################################################
use strict;
my $type="host";

my $hostname = `/bin/uname -n`; chomp $hostname;
my $hostid = `/usr/bin/hostid`; chomp $hostid;
my $date = `/bin/date +"%Y-%m-%d %H:%M:%S"`; chomp $date;
my @PS = `/bin/ps -eo args`;  
my @TMP;                                                                                                                    

my $outfile = "/var/OPM/data/${hostname}_${type}.out";
open (OUT, ">$outfile");
print OUT '<uname -n>',"\n",$hostname,"\n",'</uname -n>', "\n";
print OUT '<hostid>',"\n",$hostid,"\n",'</hostid>', "\n";
print OUT '<date>',"\n",$date,"\n",'</date>', "\n";

my %CONNECTIONS;
my %HOSTFILE;
my @a_ports_sorted;
########################################################################
# Sub's
########################################################################
sub Network {
	my $ip = shift;
	my $mask = shift;
	my @IP = split /\./,$ip;
	my @MASK; if ($mask =~ /(\w\w)(\w\w)(\w\w)(\w\w)/){@MASK = (hex($1),hex($2),hex($3),hex($4));}
	print "ip =$ip mask=$mask\n";
	my $net = shift @IP & shift @MASK;
	$net .= ".".(shift @IP & shift @MASK);
	$net .= ".".(shift @IP & shift @MASK);
	$net .= ".".(shift @IP & shift @MASK);
	return $net;
}
sub DnsName {
   my $ip = shift;
   my $lookup = `/usr/bin/nslookup $ip 2>&1`;
   my $iname = "-";
   my $idomain = "-";
   $lookup =~ s/\n/ /g;
   $lookup =~ s/\t/ /g;
   $lookup =~ s/\s+/ /g;
   if ($lookup =~ /name\s=\s(.*)\.(.*)\./){
      $iname = $1;
      $idomain = $2;
   }
   return ($iname,$idomain);
}
sub OpenPorts {
   my @netstat = `/bin/netstat --listening -n`;
   my @port;
   foreach my $l (@netstat){
      if ($l =~ /^.+:(\d+)\s+.+LISTEN/){
         push(@port, $1);
      }
   }
	##################
	# Sort it and uniq
	##################
   my %h_ports;
   @h_ports{@port} = ();
   @a_ports_sorted = sort keys %h_ports;
}
sub LoadHostFile {
	my @L = `/bin/cat /etc/hosts`;
	foreach my $l (@L){
		chomp $l;
		$l =~ s/\s+/ /g;
		if    ($l =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.+?)\s+.*#(.*)/){ $HOSTFILE{$1} = "$2\t$3"; }
		elsif ($l =~ /^(\d+\.\d+\.\d+\.\d+)\s+(.+)/){ $HOSTFILE{$1} = "$2\t-"; }
	}
}

sub ProcessRunning{
        my @LIST = @_;
        my $result = "no";
        my $count = 0;
        foreach my $PS (@PS){foreach my $pattern (@LIST){if ($PS =~ /$pattern/){
 $count ++; $result = "yes";}}}
        return ($result, $count);
}
sub Virtual{
	print OUT "<vm>\n";
	my @vm = `qm list | grep -v VMID`;
	foreach my $row (@vm){
		chomp $row;
		$row =~ s/\s+/ /g; $row =~ s/^\s+//g;
		my ($id, $name, $status, $memory, $bootdisk, $pid) = split /\s/, $row;
		print OUT "$id\t$name\t$status\t$memory\t$bootdisk\t$pid\n";
	}
	print OUT "</vm>\n";
}
########################################################################
# MAIN
########################################################################
# Pour extraire les info pour info_host
{
	print OUT "<host>\n";
	my $unamei = `/bin/uname -i`;chomp $unamei;
	my $prtdiag = `cat /proc/meminfo|grep "MemTotal:"`; chomp $prtdiag;
	##################
	# number of cpu's
	##################
	my $psrinfo = `cat /proc/cpuinfo |grep processor|wc -l`; chomp $psrinfo;
	##################
	my $netactive = `/sbin/ip link show | grep '^.:' | grep -v lo | grep UP | wc -l`; chomp $netactive;
	my $netinactive = `/sbin/ip link show | grep '^.:' | grep -v lo | grep DOWN | wc -l`; chomp $netinactive;
	my $fcsan	= '0';
	my $fcnotsan	= '0';
	my $scsi	= `/sbin/lspci|grep -i scsi|wc -l`;chomp $scsi;
	my $san		= `/sbin/lspci|grep -i fiber|wc -l`;chomp $san;
	#my $os = `cat /etc/redhat-release`; chomp $os; chomp $os;
	my $os = `cat /etc/issue`; chomp $os; chomp $os; if ( $os =~ /^([a-zA-Z]+) / ){ $os = $1; }
	my $kernel = `/bin/uname -r`; chomp $kernel;
	my $class = `/bin/uname -m`; chomp $class;
	my $proctype = `cat /proc/cpuinfo | grep 'model name' | head -1 | cut -d':' -f2 | sed 's/^ //g'`; chomp $proctype;
	#my $platform = `/bin/uname -i`; chomp $platform;
	my $platform = "Intel";
	`ssh -V > /tmp/sshversion 2>&1`;
   my $ssh = `/bin/cat /tmp/sshversion`;chomp $ssh;if ( $ssh =~ /(.*),.*/ ){ $ssh = $1; }
	my ($ram,$unit) = (0,"-");
	if ($psrinfo =~ /(\d+)/){$psrinfo = $1;}
	if ($prtdiag =~ /MemTotal.+?\s+(\d+)\s*(\w+)/){
		($ram,$unit) = ($1,$2);
		if ($unit =~ /[Mm]/){$ram = $ram/1024;} 
		if ($unit =~ /[Kk]/){$ram = $ram/1024/1024;}
	}
	print OUT "$ram\t$psrinfo\t$os\t$kernel\t$class\t$proctype\t$platform\t$san\t$netactive\t$netinactive\t$fcsan\t$fcnotsan\t$scsi\t$ssh\n";
	print OUT "</host>\n";
}
{
	print OUT "<routes>\n";
	my @TMP = `/bin/netstat -nr`; 
	shift @TMP;shift @TMP;shift @TMP;shift @TMP;
	foreach my $route (@TMP){
		my ($ref,$use) = ('-','-');
		my ($dest,$gw,$genmask,$flags,$mss,$window,$irtt,$i) = split /\s+/, $route;
		if( !defined($i)){ $i = "-"; }
		print OUT "$dest\t$gw\t$flags\t$ref\t$use\t$i\n";
	}
	print OUT "</routes>\n";
}
{
	print OUT "<ip>\n";
	OpenPorts();
	# sets @a_ports_sorted
	LoadHostFile();
	my $tmp = `/sbin/ifconfig`; $tmp =~ s/^\t+//g; $tmp =~ s/ +/ /g;
	my @interfaces = split /.*\n\n/, $tmp;
	foreach my $eth (@interfaces){
		next if ( $eth =~ /^lo/ );
		my ($ifname,$inet,$netmask,$broadcast,$ether,$listeners)=('NA','NA','NA','NA','NA','NA');
		my @LINES = split /\n/, $eth;
		if ($LINES[0] =~ /^([\w:]+)\s/){$ifname = $1;}
		if ($LINES[0] =~ /HWaddr\s([\w:]+)\s/){$ether = $1;}
		if ($LINES[1] =~ /inet addr:([\w\.]+)\s/){$inet = $1;}
		if ($LINES[1] =~ /Mask:([^\s]+)/){$netmask = $1;}
		if ($LINES[1] =~ /Bcast:([^\s]+)/){$broadcast = $1;}
		my ($name,$domain) = DnsName($inet);
		my $info = "";
		my $hostfile = exists($HOSTFILE{$inet})?$HOSTFILE{$inet}:"Not in hosts file\t-";
		my ($hostfileentry,$comment) = split /\t/, $hostfile; 
		my $ifbase = $ifname; $ifbase =~ s/:\d+$//;
		if ($ifname =~ /.*:\d+/){$info .= ",VIP";} else {$info .= ",PHY";};
		if ($hostname eq $name){$info .= ",HOST-IP";}
		$info =~ s/^,//;
		print OUT "$ifname\t$inet\t$netmask\t$broadcast\t$ether\t$info\t$name\t$domain\t$hostfileentry\t$comment\n";
	}
	print OUT "</ip>\n";
}
{
   print OUT "<process>\n";
   my @PS = `/bin/ps -eo user,args | sort | uniq`;
   foreach my $PS (@PS){
                $PS =~ s/\s+/ /g;
      chomp $PS;
      my @a_PS = split / /, $PS;
      my $user = shift(@a_PS);
      #my $command = join(" ", @a_PS);
      print OUT "$user\t@a_PS\n";
   }
   print OUT "</process>\n";
}
if ( -x "/usr/sbin/qm" ) { Virtual; }
close OUT;
`chown saunix $outfile`;
